package com.tanhua.server.controller;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.server.service.MovementsMQService;
import com.tanhua.server.service.MovementsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("movements")
@Slf4j
public class MovementsController {
    @Autowired
    private MovementsService movementsService;
    @Autowired
    private MovementsMQService movementsMQService;
    
    /**
     * 接口名称：动态-发布
     * 接口路径：POST/movements
     * MultipartFile
     * 1. springmvc提供的一个对象，用于封装上传的文件
     * 2. 提供了获取文件名、文件流的方法
     * 3. imageContent 对应请求参数名称
     */
    @PostMapping
    public ResponseEntity<Object> saveMovements(
            Publish publish, MultipartFile[] imageContent) throws IOException {
        // 先调用service方法，为了拿到动态id
        ResponseEntity<Object> entity = movementsService.saveMovements(publish, imageContent);
        // [发送一个mq消息：操作类型1表示发布动态]
        movementsMQService.publishMsg(publish.getId().toString());
        return entity;
    }
    
    /**
     * 接口名称：好友动态
     * 接口路径：GET/movements
     * 需求分析：查询好友动态，就是刷朋友圈，查询时间线表. 登陆用户id是1，查询的表quanzi_timeline_1
     */
    @GetMapping
    public ResponseEntity<Object> queryPublishList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        log.info("到达了查看好友动态的方法");
        if (page < 1) {
            page = 1;
        }
        return movementsService.queryPublishList(page, pagesize);
    }
    
    /**
     * 接口名称：推荐动态
     * 接口路径：GET/movements/recommend
     * 需求分析：查询登陆用户的推荐动态 db.recommend_quanzi.find({userId:1})
     */
    @GetMapping("recommend")
    public ResponseEntity<Object> queryRecommendPublishList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        return movementsService.queryRecommendPublishList(page, pagesize);
    }
    
    
    /**
     * 接口名称：动态-点赞
     * 接口路径：GET/movements/:id/like
     * 需求分析：1、往评论表插入数据；2、修改动态表点赞数量；3、返回点赞总数
     */
    @GetMapping("{id}/like")
    public ResponseEntity<Object> likeComment(@PathVariable("id") String publishId) {
        // [发送一个mq消息：操作类型3表示点赞]
        movementsMQService.likePublishMsg(publishId);
        return movementsService.likeComment(publishId);
    }
    
    /**
     * 接口名称：动态-取消点赞
     * 接口路径：GET/movements/:id/dislike
     */
    @GetMapping("{id}/dislike")
    public ResponseEntity<Object> dislikeComment(@PathVariable("id") String publishId) {
        // [发送一个mq消息：操作类型6表示取消点赞]
        movementsMQService.disLikePublishMsg(publishId);
        return movementsService.dislikeComment(publishId);
    }
    
    /**
     * 接口名称：动态-喜欢
     * 接口路径：GET/movements/:id/love
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Object> loveComment(@PathVariable("id") String publishId) {
        // [发送一个mq消息：操作类型4表示喜欢]
        movementsMQService.lovePublishMsg(publishId);
        return movementsService.loveComment(publishId);
    }
    
    /**
     * 接口名称：动态-取消喜欢
     * 接口路径：GET/movements/:id/unlove
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Object> unloveComment(@PathVariable("id") String publishId) {
        // [发送一个mq消息：操作类型7表示取消喜欢]
        movementsMQService.disLovePublishMsg(publishId);
        return movementsService.unloveComment(publishId);
    }
    
    /**
     * 接口名称：单条动态
     * 接口路径：GET/movements/:id
     */
    @GetMapping("{id}")
    public ResponseEntity<Object> queryMovementsById(@PathVariable("id") String publishId) {
        return movementsService.queryMovementsById(publishId);
    }
    
    /**
     * 接口名称：谁看过我
     * 接口路径：GET/movements/visitors
     */
    @GetMapping("visitors")
    public ResponseEntity<Object> queryVisitors() {
        return movementsService.queryVisitors();
    }
    
    
    /**
     * 接口名称：用户动态 (我的动态)
     * 接口路径：GET/movements/all
     */
    @GetMapping("all")
    public ResponseEntity<Object> queryAlbumList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pagesize, Long userId) {
        //前端有一个bug，page = 0
        page = page < 1 ? 1 : page;
        return movementsService.queryAlbumList(page, pagesize, userId);
    }
}